<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>

<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>静态化页面管理</title>
    <script src="${basePath}/static/plugins/jq/jquery-2.2.4.min.js"></script>
    <link href="${basePath }/static/plugins/layui/css/layui.css" rel="stylesheet"/>
</head>
<body>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>静态化页面管理界面</legend>
</fieldset>

<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">输入想要静态化页面的url</label>
        <div class="layui-input-block">
            <input id="submitUrl" type="text" name="url" lay-verify="title" autocomplete="off" placeholder="请输入相对路径的url" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" onclick="staticizePage()">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<div id="main">
    <table id="table" lay-filter="table"></table>
</div>

<script src="${basePath}/static/plugins/layui/layui.js"></script>
<script>

    var table = null;
    var layer = null;
    var form = null;
    var url = '${basePath}/staticizing/getAllStaticizingUrls';

    $(function () {
        layui.use(['table', 'layer'], function(){
            table = layui.table;
            layer = layui.layer;
            form = layui.form;

            table.on('tool(table)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的DOM对象

                if(layEvent === 'del'){ //删除
                    layer.confirm('真的删除此url吗?', function(index){
                        removeStaticizingPage(data.url);//删除
                        layer.close(index);
                    });
                }
            });
            getData();
        });
    });

    function removeStaticizingPage(url){
        $.post('${basePath}/staticizing/removeStaticizePage',{url:url},function (resultData) {
            layer.msg(resultData.resultMessage+'<br>'+resultData.resultObject, {
                time: 20000, //20s后自动关闭
                btn: ['知道了']
            });
            if (resultData.resultCode == 1){
                getData();
            }
        });
    }

    function staticizePage(){
        $.post('${basePath}/staticizing/staticizePage',{url:$("#submitUrl").val()},function (resultData) {

            layer.msg(resultData.resultMessage+'<br>'+resultData.resultObject, {
                time: 20000, //20s后自动关闭
                btn: ['知道了']
            });
            if (resultData.resultCode == 1){ //頁面靜態化成功
                getData();
            }
        });
    }

    /**
     * 获取已静态化的页面的数据
     */
    function getData(){
        $.get(url,function (data) {
            if (data!=null){
                if (typeof data == 'string'){
                    data = $.parseJSON(data);
                }
                initTable(data);//加载静态化页面数据到表格中
            }
        });
    }

    /**
     * 加载静态化页面数据到表格中
     * @param data
     */
    function initTable(data){
        var tableData = [];
        $.each(data,function (index,item) {
            tableData.push({url:item});
        });
        console.log(tableData);
        table.render({
            elem: '#table'
            ,height: 'full'
            ,limit: 12
            ,data:tableData
            ,page: true //开启分页
            ,cols: [[ //表头
                {field: 'url', title: '已被静态化的页面', width:'80%',sort:true},
                {title: '操作', width:'20%',toolbar:'#rowbtn'},
            ]]
        });
    }



</script>
<script type="text/html" id="rowbtn">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</body>
</html>